//
//  ZCOrderPayHeadView.m
//  ZCJZ
//
//  Created by sujeking on 2019/2/27.
//  Copyright © 2019年 jeking. All rights reserved.
//

#import "ZCOrderPayHeadView.h"
#import "ZCOrderConst.h"
#import "TBCityIconFont.h"
#import <Masonry.h>
@interface ZCOrderPayHeadView()

@property (weak, nonatomic) IBOutlet UIImageView *imv;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) CAGradientLayer *gLayer;
@end


@implementation ZCOrderPayHeadView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupSubViews];
}

- (void)dealloc {
    [self.timer invalidate];
    self.timer = nil;
}

- (void)setupSubViews {
  self.backgroundColor = [generalToolBox getColorWithHexadecimalColorStr:@"0xE97E89"];
    UIImage *img = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e640", 25, [generalToolBox getColorWithHexadecimalColorStr:@"0x333333"])];
    self.imv.image = img;
    self.imv.backgroundColor = [UIColor clearColor];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self configGradientLayer];
}

- (void)configGradientLayer {
    if (self.gLayer) {
        [self.gLayer removeFromSuperlayer];
    }
    CAGradientLayer *gLayer = [CAGradientLayer layer];
    gLayer.frame = self.bounds;
    gLayer.colors = @[(__bridge id)[generalToolBox getColorWithHexadecimalColorStr:@"0xF7EAE8"].CGColor,
                      (__bridge id)[generalToolBox getColorWithHexadecimalColorStr:@"0xE7D3CD"].CGColor,];
    gLayer.startPoint = CGPointMake(0, 0);
    gLayer.endPoint   = CGPointMake(1, 0);
    self.gLayer = gLayer;
    [self.layer insertSublayer:gLayer atIndex:0];
}

- (void)setDict:(NSDictionary *)dict {
    _dict = dict;
    self.statusLabel.text = [dict valueForKey:@"status_name"];
    __block NSInteger sc = [[dict valueForKey:@"pay_overtime"] integerValue];
    self.status = [[dict valueForKey:@"status"] intValue];
    if (self.status == STATUS_WAIT_PAY) {
        if (sc == 0) {
            self.timeLabel.text = @"剩余支付时间:0分0秒";
            return;
        }
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
            NSInteger mi = sc / 60;
            NSInteger scs = sc % 60;
            self.timeLabel.text = [NSString stringWithFormat:@"剩余支付时间:%zd分%zd秒",mi,scs];
            sc--;
        }];
        NSRunLoop *loop = [NSRunLoop currentRunLoop];
        [loop addTimer:self.timer forMode:NSRunLoopCommonModes];
    } else {
        if (self.status != STATUS_WAIT_GET) {
            [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.imv.mas_centerY).priorityHigh();
                make.left.mas_equalTo(self.imv.mas_right).offset(15);
            }];
            self.timeLabel.hidden = YES;
        }
    }
}

- (void)setStatus:(NSInteger)status {
    _status = status;
    switch (status) {
        case STATUS_CANCEL:
        {
            UIImage *img = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e642", 25, [generalToolBox getColorWithHexadecimalColorStr:@"0x333333"])];
            self.imv.image = img;
            self.timeLabel.hidden = YES;
            [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.imv.mas_centerY).priorityHigh();
                make.left.mas_equalTo(self.imv.mas_right).offset(15);
            }];
        }
            break;
        case STATUS_CLOSE:
        {
            UIImage *img = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e642", 25, [generalToolBox getColorWithHexadecimalColorStr:@"0x333333"])];
            self.imv.image = img;
            self.timeLabel.hidden = YES;
            [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.imv.mas_centerY).priorityHigh();
                make.left.mas_equalTo(self.imv.mas_right).offset(15);
            }];
        }
            break;
        case STATUS_WAIT_SEND:
        {
            UIImage *img = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e641", 25, [generalToolBox getColorWithHexadecimalColorStr:@"0x333333"])];
            self.imv.image = img;
            
            self.timeLabel.hidden = YES;
            [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.imv.mas_centerY).priorityHigh();
                make.left.mas_equalTo(self.imv.mas_right).offset(15);
            }];
        }
            break;
        case STATUS_WAIT_GET:
        {
            UIImage *img = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e641", 25, [generalToolBox getColorWithHexadecimalColorStr:@"0x333333"])];
            self.imv.image = img;
            __block NSInteger sc = [[self.dict valueForKey:@"auto_complete"] integerValue];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSInteger mi = sc % 60;
                NSInteger hour = (sc / 3600) % 24;
                NSInteger day = (sc / 3600) / 24;
                self.timeLabel.text = [NSString stringWithFormat:@"还有%zd天%zd时%zd分钟自动确认收货",day,hour,mi];
            });
            
            self.timer = [NSTimer scheduledTimerWithTimeInterval:60 repeats:YES block:^(NSTimer * _Nonnull timer) {
                if (sc <= 0) {
                    [self.timer invalidate];
                } else {
                    sc--;
                }
                NSInteger mi = sc % 60;
                NSInteger hour = (sc / 3600) % 24;
                NSInteger day = (sc / 3600) / 24;
                self.timeLabel.text = [NSString stringWithFormat:@"还有%zd天%zd时%zd分钟自动确认收货",day,hour,mi];
            }];
            NSRunLoop *loop = [NSRunLoop currentRunLoop];
            [loop addTimer:self.timer forMode:NSRunLoopCommonModes];
        }
            break;
        case STATUS_WAIT_PAY:
        {
            UIImage *img = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e640", 25, [generalToolBox getColorWithHexadecimalColorStr:@"0x333333"])];
            self.imv.image = img;
        }
            break;
        case STATUS_WAIT_FEEDBACK:
        {
            UIImage *img = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e644", 25, [generalToolBox getColorWithHexadecimalColorStr:@"0x333333"])];
            self.imv.image = img;
            self.timeLabel.hidden = YES;
            [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.imv.mas_centerY).priorityHigh();
                make.left.mas_equalTo(self.imv.mas_right).offset(15);
            }];
        }
            break;
        case STATUS_END:
        {
            UIImage *img = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e644", 25, [generalToolBox getColorWithHexadecimalColorStr:@"0x333333"])];
            self.imv.image = img;
            [self.timer invalidate];
            self.timer = nil;
            self.timeLabel.text = @"已收货";
        }
            break;
        default:
            break;
    }
}

@end

